// packageA/pages/iotException/iotException.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
const util = require('../../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参来的信息
    paramFrom:null,
    //传参来的imei
    imei:'',
    //列表
    list:[],
    //页码
    page: 1,
    //是否无更多
    notMore: true,
    //无数据
    showNothing:false,
    //搜索关键词
    searchValue:'',
    //删除确认框
    showDelete:false,
    //删除的id
    deleteId:null,
    //状态
    stateOption: [
      {text:'未处理',value: 0},
      {text:'已处理',value: 1},
      {text:'所有记录',value: 2}
    ],
    state: 2,
    //是否显示搜索
    showSearch:true,
    //用户权限
    authority:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取传参
    if(options.paramFrom){
      let obj=JSON.parse(options.paramFrom);
      this.setData({
        paramFrom:obj,
        imei: obj.imei,
        showSearch:false
      })
    }

    this.setData({
      authority:app.globalData.authority
    })

    //获取列表数据
    this.getList();
  },

  //获取列表数据
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param = {
      isAuth: true,
      className: 'iotException',
      data: {
        selectFields: ['id','imei','startTime','endTime','typeName','describe','contactPerson','reason','imgs','createDate','state','dealUserName','result','remarks','dealTime','phone'],
        sort: {
          createDate: -1
        },
        page: {
          page: this.data.page,
          pageSize: 10
        }
      }
    }

    //imei查询
    if(this.data.imei){
      param.data.imei=this.data.imei;
    }

    //状态筛选
    if(this.data.state!=2){
      param.data.state=this.data.state;
    }

    //imei模糊查询
    if(this.data.searchValue){
      param.data.imei='%'+this.data.searchValue+'%';
    }

    CommonService.goFindList(param).then(res=>{
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;
        console.log('物联网数据异常');
        console.log(result);

        result.forEach(item => {
          if(item.startTime){
            item.startTime=util.timeFormat(item.startTime);
          }
          if(item.endTime){
            item.endTime=util.timeFormat(item.endTime);
          }
          if(item.dealTime){
            item.dealTime=util.timeFormat(item.dealTime);
          }
          item.createDate=util.timeFormat(item.createDate);
        });

        this.setData({
          list: result
        })
      }else{
        this.setData({
          notMore: false
        })
        if(this.data.page==1){
          this.setData({
            showNothing: true
          })
        }else{
          Toast.fail('没有更多了');
        }
      }
    }).catch((error) => {
        Toast.clear();
        this.setData({
          showNothing: true
        })
    })
  },

  //刷新列表和页码
  refreshListAndPage(){
    this.setData({
      list: [],
      page:1,
      notMore: true,
      showNothing: false
    })
    this.getList();
  },

  //获取搜索词
  onChangeSearch(e) {
    this.setData({
      searchValue: e.detail,
    })
  },

  //点击搜索
  onSearch(){
    this.refreshListAndPage();
  },

  //跳转添加
  goToAdd(){
    if(this.data.paramFrom){
      wx.navigateTo({
        url: "/packageA/pages/iotExceptionAdd/iotExceptionAdd?paramFrom="+JSON.stringify(this.data.paramFrom)
      })
    }else{
      wx.navigateTo({
        url: "/packageA/pages/iotExceptionAdd/iotExceptionAdd"
      })
    }
  },

  //显示删除确认框
  onDelete(e){
    let detail=e.currentTarget.dataset.detail;
    this.setData({
      showDelete: true,
      deleteId:detail.id
		})
  },

  //隐藏删除确认框
  onCloseDelete(){
    this.setData({
			showDelete: false
		})
  },

  //确认删除
  confirmDelete(){
    let param={
      className:"iotException",
      data:{
        id:this.data.deleteId
      }
    }

    CommonService.goDelete(param).then(res=>{
      if (res.statusCode == 200 && res.data.success == true){
        Toast.success('已删除');
        //刷新列表和页码
        this.refreshListAndPage();
      }else{
        Toast.fail('操作失败');
      }
    }).catch((error) => {
      Toast.fail('操作失败');
    })
  },

  //预览图片
  onPreviewImage(e) {
    let detail = e.target.dataset.detail;
    let src = e.target.dataset.src;
    wx.previewImage({
      current: src,
      urls: detail
    })
  },

  //跳转处理
  goToDeal(e){
    let detail=e.currentTarget.dataset.detail;
    wx.navigateTo({
      url: "/packageA/pages/iotExceptionDeal/iotExceptionDeal?id="+detail.id
    })
  },

  //选择状态
  onChangeState({detail}) {
    this.setData({
      state: detail
    })
    this.refreshListAndPage();
  },

  //跳转设备定位
  goToEquipment(e){
    let detail=e.currentTarget.dataset.detail;
    wx.navigateTo({
      url: "/pages/iotEquipmentDetail/iotEquipmentDetail?imei="+detail.imei
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.notMore){
      this.setData({
        page: this.data.page + 1
      })
      this.getList();
    }
  }
})
